using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace sforceExplorer
{
	/// <summary>
	/// Summary description for dbRepConfig.
	/// </summary>
	public class dbRepConfig : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TabControl tabControl1;
		private System.Windows.Forms.TabPage tabPage1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.NumericUpDown nudInterval;
		private System.Windows.Forms.ToolTip toolTip1;
		private System.Windows.Forms.CheckBox chkUpdate;
		private System.Windows.Forms.CheckBox chkDelete;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.Button btnCancel;
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.NumericUpDown nudStartTimeOffset;
		private ApiBrowserControl.SelectListChangedArguments[] selectList;
		private sforceExplorer.ApiBrowserControl apiBrowserControl1;
		private System.Windows.Forms.TextBox txtSelected;
		private System.Windows.Forms.Label label5;

		private Hashtable dataCache = DataCache.Cache;

		public dbRepConfig()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			this.Activated +=new EventHandler(dbRepConfig_Activated);
		}

		private void SaveSettings() 
		{
			Utility.RepConfig.interval = nudInterval.Value; 
			Utility.RepConfig.updates = chkUpdate.Checked;
			Utility.RepConfig.deletes = chkDelete.Checked;
			Utility.RepConfig.StartTimeOffset = Convert.ToInt16(nudStartTimeOffset.Value);
			Utility.RepConfig.RetrieveObjects = this.selectList;
			Utility.RepConfig.entities.Clear();
			for (int i=0;i<this.selectList.Length;i++) 
				Utility.RepConfig.entities.Add(this.selectList[i].TableName.ToString());
			Utility.RepConfig.Save();
			
			
		}

		private void LoadEntityList()
		{
			this.Activated -=new EventHandler(dbRepConfig_Activated);

			for (int i=0;i<Utility.GetObjectTypes().Count;i++)
			{
				string sobjectType = (string)Utility.GetObjectTypes()[i];
				if (Utility.TableIsReplicateable(sobjectType)) 
				{
//					this.lbItems.Items.Add(sobjectType);
				}
			}
//			for (int index=0;index<Utility.RepConfig.entities.Count;index++) 
//			{
//				lbItems.SetItemChecked(lbItems.FindStringExact((string)Utility.RepConfig.entities[index]), true);
//			}
			LoadRetrieveObjects();
			this.nudStartTimeOffset.Value = Convert.ToDecimal(Utility.RepConfig.StartTimeOffset);
			this.nudInterval.Value = Convert.ToDecimal(Utility.RepConfig.interval);
			this.chkUpdate.Checked = Utility.RepConfig.updates;
			this.chkDelete.Checked = Utility.RepConfig.deletes;

			((frmMain)this.Owner).sbDBRep.Panels[0].Text = "Ready...";
		}

		private void LoadRetrieveObjects() 
		{
			this.apiBrowserControl1.SelectListChanged -=new SelectListChangedEventHandler(apiBrowserControl1_SelectListChanged);
			apiBrowserControl1.Describe(false);
			if (selectList != null && selectList.Length > 0) 
			{
				for (int i=0;i<selectList.Length;i++) 
				{
					for (int j=0;j<selectList[i].SelectedFields.Length;j++) 
					{
						System.Diagnostics.Trace.WriteLine(j.ToString() + ". " + selectList[i].SelectedFields[j]);
						apiBrowserControl1.SetItemChecked(selectList[i].TableName, selectList[i].SelectedFields[j].ToString());
					}
				}
			}
			this.apiBrowserControl1.tvDesc.CollapseAll();
			this.apiBrowserControl1.SelectListChanged +=new SelectListChangedEventHandler(apiBrowserControl1_SelectListChanged);
		}
		private void SetSummaryText() 
		{
			this.txtSelected.Text = "";
			for (int i=0;i<this.selectList.Length;i++)
				this.txtSelected.Text += this.selectList[i].TableName + "(" + this.selectList[i].SelectedFields.Length.ToString() + " fields)" + System.Environment.NewLine;
		}
		private void LoadSettings() 
		{
			Cursor = Cursors.WaitCursor;
			((frmMain)this.Owner).sbDBRep.Panels[0].Text = "Loading Replicatable Entities...";
			Utility.RepConfig.Load();
			this.selectList = Utility.RepConfig.RetrieveObjects;
			//LoadRetrieveObjects();
			this.LoadEntityList();
			this.SetSummaryText();
			//System.Threading.ThreadStart ts = new System.Threading.ThreadStart(LoadEntityList);
			//loadThread = new System.Threading.Thread(ts);
			//lblLoading.Visible = true;
			//System.Windows.Forms.Application.DoEvents();
			//loadThread.Start();
			Cursor = Cursors.Default;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.tabControl1 = new System.Windows.Forms.TabControl();
			this.tabPage1 = new System.Windows.Forms.TabPage();
			this.label3 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.chkUpdate = new System.Windows.Forms.CheckBox();
			this.nudInterval = new System.Windows.Forms.NumericUpDown();
			this.label1 = new System.Windows.Forms.Label();
			this.chkDelete = new System.Windows.Forms.CheckBox();
			this.nudStartTimeOffset = new System.Windows.Forms.NumericUpDown();
			this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
			this.btnOK = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.apiBrowserControl1 = new sforceExplorer.ApiBrowserControl();
			this.txtSelected = new System.Windows.Forms.TextBox();
			this.label5 = new System.Windows.Forms.Label();
			this.tabControl1.SuspendLayout();
			this.tabPage1.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.nudInterval)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.nudStartTimeOffset)).BeginInit();
			this.SuspendLayout();
			// 
			// tabControl1
			// 
			this.tabControl1.Controls.Add(this.tabPage1);
			this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.tabControl1.Location = new System.Drawing.Point(0, 0);
			this.tabControl1.Name = "tabControl1";
			this.tabControl1.SelectedIndex = 0;
			this.tabControl1.Size = new System.Drawing.Size(520, 459);
			this.tabControl1.TabIndex = 0;
			// 
			// tabPage1
			// 
			this.tabPage1.Controls.Add(this.label5);
			this.tabPage1.Controls.Add(this.txtSelected);
			this.tabPage1.Controls.Add(this.apiBrowserControl1);
			this.tabPage1.Controls.Add(this.label3);
			this.tabPage1.Controls.Add(this.label2);
			this.tabPage1.Controls.Add(this.chkUpdate);
			this.tabPage1.Controls.Add(this.nudInterval);
			this.tabPage1.Controls.Add(this.label1);
			this.tabPage1.Controls.Add(this.chkDelete);
			this.tabPage1.Controls.Add(this.nudStartTimeOffset);
			this.tabPage1.Location = new System.Drawing.Point(4, 22);
			this.tabPage1.Name = "tabPage1";
			this.tabPage1.Size = new System.Drawing.Size(512, 433);
			this.tabPage1.TabIndex = 0;
			this.tabPage1.Text = "Polling";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(224, 16);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(128, 16);
			this.label3.TabIndex = 6;
			this.label3.Text = "Start time offset (mins):";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 96);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(240, 32);
			this.label2.TabIndex = 4;
			this.label2.Text = "Which items do you want to poll?  Select individual fields to return during updat" +
				"e polling.";
			// 
			// chkUpdate
			// 
			this.chkUpdate.Checked = true;
			this.chkUpdate.CheckState = System.Windows.Forms.CheckState.Checked;
			this.chkUpdate.Location = new System.Drawing.Point(16, 48);
			this.chkUpdate.Name = "chkUpdate";
			this.chkUpdate.Size = new System.Drawing.Size(144, 16);
			this.chkUpdate.TabIndex = 2;
			this.chkUpdate.Text = "Get Updates";
			// 
			// nudInterval
			// 
			this.nudInterval.Location = new System.Drawing.Point(144, 16);
			this.nudInterval.Maximum = new System.Decimal(new int[] {
																		60,
																		0,
																		0,
																		0});
			this.nudInterval.Minimum = new System.Decimal(new int[] {
																		1,
																		0,
																		0,
																		0});
			this.nudInterval.Name = "nudInterval";
			this.nudInterval.Size = new System.Drawing.Size(40, 20);
			this.nudInterval.TabIndex = 1;
			this.toolTip1.SetToolTip(this.nudInterval, "Pick a number between 1 and 60.");
			this.nudInterval.Value = new System.Decimal(new int[] {
																	  5,
																	  0,
																	  0,
																	  0});
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(136, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "Polling Interval (minutes)";
			// 
			// chkDelete
			// 
			this.chkDelete.Location = new System.Drawing.Point(16, 72);
			this.chkDelete.Name = "chkDelete";
			this.chkDelete.Size = new System.Drawing.Size(144, 16);
			this.chkDelete.TabIndex = 2;
			this.chkDelete.Text = "Get Deletes";
			// 
			// nudStartTimeOffset
			// 
			this.nudStartTimeOffset.Location = new System.Drawing.Point(352, 16);
			this.nudStartTimeOffset.Maximum = new System.Decimal(new int[] {
																			   60,
																			   0,
																			   0,
																			   0});
			this.nudStartTimeOffset.Minimum = new System.Decimal(new int[] {
																			   60,
																			   0,
																			   0,
																			   -2147483648});
			this.nudStartTimeOffset.Name = "nudStartTimeOffset";
			this.nudStartTimeOffset.Size = new System.Drawing.Size(40, 20);
			this.nudStartTimeOffset.TabIndex = 1;
			this.toolTip1.SetToolTip(this.nudStartTimeOffset, "Pick a number between 1 and 60.");
			// 
			// btnOK
			// 
			this.btnOK.Location = new System.Drawing.Point(320, 464);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(88, 24);
			this.btnOK.TabIndex = 1;
			this.btnOK.Text = "OK";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.Location = new System.Drawing.Point(424, 464);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(88, 24);
			this.btnCancel.TabIndex = 1;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// apiBrowserControl1
			// 
			this.apiBrowserControl1.CurrentObject = null;
			this.apiBrowserControl1.CurrentRootNode = null;
			this.apiBrowserControl1.Location = new System.Drawing.Point(16, 136);
			this.apiBrowserControl1.MultiObjectSelect = true;
			this.apiBrowserControl1.Name = "apiBrowserControl1";
			this.apiBrowserControl1.Size = new System.Drawing.Size(272, 272);
			this.apiBrowserControl1.TabIndex = 7;
			// 
			// txtSelected
			// 
			this.txtSelected.Location = new System.Drawing.Point(304, 136);
			this.txtSelected.Multiline = true;
			this.txtSelected.Name = "txtSelected";
			this.txtSelected.ReadOnly = true;
			this.txtSelected.Size = new System.Drawing.Size(192, 280);
			this.txtSelected.TabIndex = 8;
			this.txtSelected.Text = "";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(304, 112);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(152, 16);
			this.label5.TabIndex = 9;
			this.label5.Text = "Currently Selected Objects";
			// 
			// dbRepConfig
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(520, 494);
			this.ControlBox = false;
			this.Controls.Add(this.btnOK);
			this.Controls.Add(this.tabControl1);
			this.Controls.Add(this.btnCancel);
			this.DockPadding.Bottom = 35;
			this.Name = "dbRepConfig";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Replication Configuration";
			this.tabControl1.ResumeLayout(false);
			this.tabPage1.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.nudInterval)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.nudStartTimeOffset)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void btnOK_Click(object sender, System.EventArgs e)
		{
			SaveSettings();
			DialogResult = DialogResult.OK;
			Close();
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			Cursor = Cursors.WaitCursor;
			Cursor = Cursors.Default;
			DialogResult = DialogResult.Cancel;
			Close();
		}

		private void dbRepConfig_Activated(object sender, EventArgs e)
		{
			LoadSettings();
		}

		private void apiBrowserControl1_SelectListChanged(object sender, ApiBrowserControl.SelectListChangedArguments[] e)
		{
			System.Diagnostics.Trace.WriteLine("Select list changed.");
			this.selectList = e;
			this.SetSummaryText();
		}
	}
}
